package protocol.xml;

public class XmlParser {

	/**
	 * Util method to find the value from an xml string by tag
	 * @param source
	 * @param tag
	 * @return The value from the tag read
	 */
	public static String readByTag(String source, String tag, int from) {
		String value = null;
		int beginIndex = source.indexOf("<"+tag+">", from);
		if(beginIndex >= 0) {
			int endIndex = source.indexOf("</"+tag+">", beginIndex+2+tag.length());
			if(endIndex >= 0) {
				value = source.substring(beginIndex+tag.length()+2, endIndex);
			}
		}
		return value;
	}
	
	public static int guessNextLimit(String tag, String value, int lastIndex) {
		try {
			return lastIndex + tag.length()*2+5+value.length();
		} catch(Exception e) {
			return lastIndex;
		}
	}
	
//	public static String readByTag(String source, String tag, int from, Integer endOfTag) {
//		int beginIndex = source.indexOf("<"+tag+">", from);
//		if(beginIndex >= 0) {
//			int endIndex = source.indexOf("</"+tag+">", beginIndex+1);
//			if(endIndex > 0) {
//				endOfTag.valueOf(endIndex + tag.length() + 1);
//				return source.substring(beginIndex+tag.length()+2, endIndex);
//			}
//		}
//		return null;
//	}

}
